package com.arcaneshift.servlet.util.parser;

import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.arcaneshift.model.request.SummonCreatureCardRequest;
import com.arcaneshift.servlet.abstracts.XMLParserBase;
import com.arcaneshift.servlet.util.network.HTTPProtocol;
import com.arcaneshift.util.Converter;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractResponse;
import com.common.security.exceptions.ConverterException;
import com.common.security.exceptions.HTTPException;
import com.common.security.exceptions.ParserException;

public class SummonCreatureCardXMLParser extends XMLParserBase {

	@Override
	public AbstractRequest parseRequest(InputStream stream) throws HTTPException, ParserException {
		SummonCreatureCardRequest request = new SummonCreatureCardRequest();
		
		Document document = createDocument(stream);
		
		Node actionNode = getActionNode(document);
		Node contentNode = getContentNode(document);
		
		insertAction(actionNode, request);
		Element element = getContent(contentNode);
		
		Element session = getElement(element, HTTPProtocol.SESSION);
		Element game = getElement(element, HTTPProtocol.GAME);
		Element card = getElement(element, HTTPProtocol.CARD);
		
		request.setSessionId(session.getAttribute(HTTPProtocol.ATTRIBUTE_VALUE));
		request.setGameId(game.getAttribute(HTTPProtocol.ATTRIBUTE_VALUE));
		request.setCardId(card.getAttribute(HTTPProtocol.ATTRIBUTE_VALUE));
		
		try {
			request.setXPosition(Converter.convertToInt(card.getAttribute(HTTPProtocol.ATTRIBUTE_X_POSITION)));
			request.setXPosition(Converter.convertToInt(card.getAttribute(HTTPProtocol.ATTRIBUTE_Y_POSITION)));
		} catch (ConverterException e) {
			throw new ParserException(ParserException.CONVERSION_EXCEPTION);
		}
		
		return request;
	}

	@Override
	public String parseResponse(AbstractResponse abstractResponse) throws ParserException {
		// TODO Auto-generated method stub
		return null;
	}
}